const http = require('./utils/http.js');
const util = require('./utils/util_method.js');
const app_method = require('./utils/app_method.js');
var code,rawData = {};
App({
  onLaunch: function () {
  
  },
  apiUrl: 'http://niu.test.tsingc.com/api',
  stateUrl: 'http://niu.test.tsingc.com',
  globalData: {
    showDialog:null,
    userInfo: app_method.getUser(),
    timeDifference: 1,	// 前端与服务端时差
    cate_id: 0,  //牛分类页面id
    parent_id: 0,
    // 城市编号
    city: 1,		// 这里必须填写默认城市编号,避免出现问题
  },
  http:http,
  util:util,
  ...app_method,
  //微信登陆
  login: function (cb) {
    let that = this;
    //调用登录接口
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.getUserInfo({
            success: function (ures) {
              http.$ajax({
                url: 'user/login_wx',
                data: {
                  code: res.code,
                  rawData: ures.rawData,
                  parent_id: that.globalData.parent_id
                },
                loading: '授权中...'
              }, {
                  success(res) {
                    that.saveUser(res.data.userInfo);
                    typeof cb == "function" && cb(res.data.userInfo);
                  },
                  fail(res) {
                    that.globalData.auth = null;
                    that.showLoginModal(cb);
                  }
                })
            },
            fail: function (res) {
              that.globalData.auth = null;

              that.showLoginModal(cb);
            }
          });
        } else {
          that.showLoginModal(cb);
        }
      }
    });
  },

});